/*
 * File Name：SpeechUtils.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： SpeechUtils.java
 * Modify By：jejun
 * Modify Date：2014-2-13
 * Modify Type：Add
 */
package com.ciwong.epaper.modules.evaluate.util;

import android.graphics.Color;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import com.ciwong.epaper.R;
import com.ciwong.libs.oralevaluate.bean.SubWord;
import com.ciwong.libs.utils.CWLog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * 语音评测工具类
 *
 * @author jejun
 * @version ciwong v.1.0 2014-2-13
 * @since ciwong v.1.0
 */
public class SpeechUtils extends com.ciwong.libs.oralevaluate.SpeechUtils {
    private final static String TAG = "SpeechUtils";

    //    private static final String WORD_REGULAR = "[-’↑↓'\"“”:：;,，!！。͜.?？/·=()]";// 正则表达式
    private static final String WORD_REGULAR = "[-↑↓\"“”：;，!！。͜.?？/·=()]";// 正则表达式

    /**
     * 单个单词或句子对应分数等级界限
     */
    public final static int SCORE_1 = 20;

    public final static int SCORE_2 = 60;

    public final static int SCORE_3 = 70;

    public final static int SCORE_4 = 80;

    public final static int SCORE_5 = -1;

    public final static int SCORE_6 = 1;


    public final static float ONE_SCORE_2 = 6.0f;

    public final static float ONE_SCORE_4 = 8f;


    public final static int SCORE_SYMBOL_2 = 6;

    public final static int SCORE_SYMBOL_3 = 7;

    public final static int SCORE_SYMBOL_4 = 8;
    /**
     * 总分对应等级
     */
    public final static int TOTAL_SCORE_1 = 60;

    public final static int TOTAL_SCORE_2 = 70;

    public final static int TOTAL_SCORE_3 = 80;

    public final static int TOTAL_SCORE_4 = 90;

    public final static int TOTAL_SCORE_5 = 1;

    /**
     * 结果页各个分数对应文字
     */
    public final static String[] SCORE_RESULT_TEXT = {"You can do better!",
            "Good!", "Well done!", "Wonderful!", "Awesome!"};

    /**
     * 各个等级的音频：0-19分
     */
//    public final static int[] LEVEL_A = {R.raw.a_level1, R.raw.a_level2};

    public final static int[] LEVEL_A = {R.raw.b_level3};

    /**
     * 各个等级的音频：20-57分
     */
    public final static int[] LEVEL_B = {R.raw.b_level1, R.raw.b_level2,
            R.raw.b_level3, R.raw.b_level4, R.raw.b_level5,};

    /**
     * 各个等级的音频：58-69分
     */
    public final static int[] LEVEL_C = {R.raw.c_level1, R.raw.c_level2,
            R.raw.c_level3, R.raw.c_level4, R.raw.c_level5};


    /**
     * 各个等级的音频：70-79分
     */
    public final static int[] LEVEL_D = {R.raw.d_level1, R.raw.d_level2,
            R.raw.d_level3, R.raw.d_level4, R.raw.d_level5};

    /**
     * 各个等级的音频：80-100分
     */
    public final static int[] LEVEL_E = {R.raw.e_level1, R.raw.e_level2,
            R.raw.e_level3, R.raw.e_level4, R.raw.e_level5};

    // public final static ImageSize WORD_IMA_SIZE = new ImageSize(
    // R.dimen.word_pic_width, R.dimen.word_pic_height);

    /**
     * 获取随机音频
     *
     * @param raws
     * @return
     */
    public static int getRandomRaw(int[] raws) {
        return raws[(int) (Math.random() * (raws.length - 1))];
    }

    public static String replaceAsSpace(String input) {
        if (!TextUtils.isEmpty(input)) {
            return input.replaceAll(WORD_REGULAR, " ").replaceAll(" +", " ")// 替换多个空格为一个空格
                    .trim();
        }
        return input;
    }

    public static String replace(String input) {
        if (!TextUtils.isEmpty(input)) {
            return input.replaceAll(WORD_REGULAR, "").replaceAll(" +", " ");
        }
        return input;
    }

    /**
     * 判断是否包含人名
     *
     * @param input
     * @return
     */
    public static int[] isMatchPersonName(String input) {
        int[] indexArray = null;
        if (!TextUtils.isEmpty(input)) {
            input = input.trim();
            //英文大括号
            if (input.contains("{") && input.contains("}")) {
                int start = input.indexOf("{");
                int end = input.indexOf("}");
                if (start < end) ;
                {
                    indexArray = new int[2];
                    indexArray[0] = start;
                    indexArray[1] = end;
                    return indexArray;
                }
            } else if (input.contains("｛") && input.contains("｝")) {
                int start = input.indexOf("｛");
                int end = input.indexOf("｝");
                if (start < end) ;
                {
                    indexArray = new int[2];
                    indexArray[0] = start;
                    indexArray[1] = end;
                    return indexArray;
                }
            }
        }
        return null;
    }

    /**
     * 过滤掉人名和翻译
     *
     * @param sentenceText 原始文字
     * @return 去除人名
     */
    public static String replaceEvaluateText(String sentenceText) {
        try {
            //去除翻译文字的大括号
            String pattern1 = "\\{.*?\\}";
            String pattern2 = "｛.*?｝";
            sentenceText = sentenceText.replaceAll(pattern1, " ").trim();
            sentenceText = sentenceText.replaceAll(pattern2, " ").trim();
            //去除开头名字的冒号
            if (sentenceText.startsWith(":")) {
                sentenceText = sentenceText.substring(0);
            }
            //去除换行符
            if (sentenceText.contains("\n")) {
                sentenceText = sentenceText.replaceAll("\n", " ");
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return sentenceText;
    }

    /**
     * 评分系数
     */
    protected static float mScoreCoefficient = 1.0f;

    public static void setScoreCoefficient(float scoreCoefficient) {
        if (scoreCoefficient > 0) {
            mScoreCoefficient = scoreCoefficient;
        }

    }

    /**
     * 优化评分结果
     *
     * @param score
     * @return
     */
    public static float scoreOptimize(float score) {

        if (mScoreCoefficient > 0 && mScoreCoefficient < 2) {
            score = score * mScoreCoefficient;
        }
        Log.e(TAG, "scoreOptimize: 评分系数==" + mScoreCoefficient + "  获得的分数==" + score);
        //超过100分，重新赋值为100分
        if (score > 100) {
            score = 100;
        }
        return score;
    }

    /**
     * 检测字符串是否包含开头与结束以'
     *
     * @param str
     * @return
     */
    public static String checkSentence(String str) {
        if (str == null || "".equals(str))
            return str;
        if (!"T-shirt".equals(str)) {
            str = str.replaceAll("-", " ");
        }
        StringBuffer text = new StringBuffer();
        String strs[] = str.split(" ");
        if (strs != null) {
            int len = strs.length;
            for (int i = 0; i < len; i++) {
                if (strs[i].startsWith("'") || strs[i].endsWith("'")
                        || strs[i].endsWith("'.") || strs[i].endsWith("',")
                        || strs[i].endsWith("'?") || strs[i].endsWith("'!")) {
                    strs[i] = strs[i].replace("'", "");
                }

                if (i != 0) {
                    text = text.append(" ");
                }
                text = text.append(strs[i]);
            }
        }
        str = text.toString();
        return str;
    }

    /**
     * 获取默认的
     */
    public static DisplayImageOptions.Builder getDefaultDisplayOptionsBuilder() {
        DisplayImageOptions.Builder defDisplayBuilder = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisc(true)
                .showImageForEmptyUri(R.mipmap.lib_default_rect_failed)
                .showImageOnFail(R.mipmap.lib_default_rect_failed)
                .showStubImage(R.mipmap.lib_default_rect);
        return defDisplayBuilder;
    }

    public static String replaceTranslateText(String processText) {
        if (TextUtils.isEmpty(processText)) {
            return processText;
        } else if (processText.endsWith("}") && processText.contains("{")) {
            int 开始 = processText.indexOf("{");
            int 结束 = processText.indexOf("}");
            String translateText = processText.substring(开始, 结束 + 1);
            processText = processText.replace(translateText, "");

        }
        return processText;
    }

    /**
     * 单词跟读模式
     */
    public static class WordReadMode {

        /**
         * 单词跟读
         */
        public static final int READ_REPEAT = 1;

        /**
         * 单词通读
         */
        public static final int READ_THROUGH = 2;
    }

    public static ForegroundColorSpan getForegroundColorSpanByScore(SubWord subWord) {
        int color;
        if (subWord.getScore() < SpeechUtils.SCORE_6) {
            color = Color.parseColor("#999999");
        }else if (subWord.getScore() < SpeechUtils.SCORE_SYMBOL_2) {
            color = Color.parseColor("#E5220E");
        } else if (subWord.getScore() < SpeechUtils.SCORE_SYMBOL_4) {
            color = Color.parseColor("#FFA800");
        } else {
            color = Color.parseColor("#1BC423");
        }
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
        return foregroundColorSpan;
    }

}
